package com.c.ctgproject.ui.production.material

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.MaterialInputWeightAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.MaterialSellEntity
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.activity_material_weight.*

/**
 *作者:LiBW
 *创建日期: 2020/10/13
 *描述:物料称重 页面
 */
class MaterialWeightActivity : BaseActivity() {
    private var page = 1
    private val size = 20
    private var searchText = ""
    private val mList = ArrayList<MaterialSellEntity.ListBean>()
    private val mAdapter: MaterialInputWeightAdapter by lazy { MaterialInputWeightAdapter(this, mList) }
    private val mViewModel: MaterialWeightViewModel by lazy { ViewModelProvider(this)[MaterialWeightViewModel::class.java] }
    override fun event() {
        mMaterialRV.adapter = mAdapter
        mMaterialRV.layoutManager = LinearLayoutManager(this)
        changeTransparentStatusBar()
        mViewModel.error.observe(this, Observer {
            loading.dismiss()
            MyLog.d("错误:$it")
            showToast(it)
        })

        mViewModel.result.observe(this, Observer {
            loading.dismiss()
            mRefresh.finishLoadMore()
            mRefresh.finishRefresh()
            MyLog.d("物料列表:$it")
            val data = initData(it)
            if (data.status == 0) {
                val entity = getGson().fromJson(data.data.handlerNull(), MaterialSellEntity::class.java).list
                entity?.let { list ->
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            } else {
                showToast(data.msg)
            }
        })
    }

    override fun onResume() {
        super.onResume()
        initData()
    }

    private fun initData() {
        mList.clear()
        page = 1
        loading.show()
        mViewModel.getMaterialList(page, size, searchText)
    }

    override fun click() {
        //上啦加载
        mRefresh.setOnLoadMoreListener {
            page++
            mViewModel.getMaterialList(page, size, searchText)
        }
        //下拉刷新
        mRefresh.setOnRefreshListener {
            mList.clear()
            initData()
        }
        //搜索
        mSearchTV.setOnClickListener {
            val search = mSearchET.text.toString()
            if (search.isNotEmpty()) {
                mList.clear()
                searchText = search
                page = 1
                loading.show()
                mViewModel.getMaterialList(page, size, search)
            } else {
                showToast("请输入需要搜索的内容")
            }
        }
        mSearchET.handlerText {
            if (it.isEmpty()) {
                searchText=""
                initData()
            }
        }
    }

    override fun getLayout() = R.layout.activity_material_weight
}